import { baseUrl } from 'src/config/env';
import { getToken } from 'src/util/auth';
import axios from "axios";

// 上传视频
export const uploadVideo = function (files, callback) {
  var form = new FormData();
  form.append("file", files); // 文件对象
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/video/v1/uploadVideo',
      method: "post",
      headers: { Authorization: getToken() },
      data: form,
      timeout: 60 * 60 * 1000
    }).then(res => {
      resolve(res);
    });
  })
}

// 上传文字
export const uploadXLSX = function (event) {
  return new Promise((resolve, rejects) => {
    var files = event.target.files[0];

    var reader = new FileReader();
    reader.onload = function (e) {
      var data = e.target.result;
      var workbook = XLSX.read(data, { type: "binary" });
      // 获取sheet1的数据
      var data = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]);
      var result = {};
      data = data.map((item) => {
        // 将数据处理成二维数组：使用name进行索引，name值与图片文件名一一对应
        // var itemData = {}
        let name = item.name;
        result[name] = {};

        for (let code in item) {
          if (code !== 'name') {
            result[name][code] = item[code];
          }
        }

        // return itemData;
      })
      resolve(result);
    };

    reader.readAsBinaryString(files);
  })
}